import * as webdriver from 'selenium-webdriver';
import * as config from './config';

import { parse_detail } from './lib'
function timeout(time) {
    return new Promise((s) => {
        setTimeout(s, time)
    })
}
const By = webdriver.By;
const until = webdriver.until;
(async function go() {
    let driver = await new webdriver.Builder().forBrowser('ie').build();
    await driver.get(config.RequestURL);

    // driver.wait(until.titleIs('中国农业银行个人网银'))
    //等待
    var iframe
    while (true) {
        try {
            let b = await driver.findElement(By.id('contentFrame'))
            await driver.switchTo().frame(b)
            console.log('登录成功，请稍后')
            break;
        } catch (error) {
            let c = error;
        }
        await timeout(5000)
        console.warn('请登录网银，并进入到明细查询页面')
    }
    await gotoDetail(driver)
    console.log('请切换到明细页面')
    // await timeout(3000)
    // await driver.executeScript('window.location.href="SingleTransferInitAct.do"')
    // https://perbank.abchina.com/EbankSite/SingleTransferInitAct.do
    // https://perbank.abchina.com/EbankSite/EbankSite/AccountTradeDetailQueryInitAct.do
    while (true) {
        try {
            let btn = await driver.findElement(By.id('btn_query'))
            await btn.click()
            await timeout(1000)
            let table = await driver.findElement(By.id('tradeDetailTable'))
            let text = await table.getText()
            parse_detail(text)
        } catch (error) {
            let a = error;
        }
        await timeout(5000)
    }

})()
/**
 * 切换到明细页面
 * @param driver 
 */
function gotoDetail(driver) {
    return new Promise(async (s) => {
        driver.executeScript('window.location.href="AccountTradeDetailQueryInitAct.do"')
        while (true) {
            try {
                await driver.findElement(By.id('btn_query'))
                // let detailBtn = await driver.findElement(By.linkText('明细'))
                // await detailBtn.click()
                // let c = await b.findElement(By.id('dCard'))
                s()
                break;
            } catch (error) {

            }
            await timeout(1000)
            console.log('等待登录成功的页面加载')
        }
    })
}
function gotoTrans(driver) {
    return new Promise(async (s) => {
        while (true) {
            try {
                await driver.executeScript('window.location.href="SingleTransferInitAct.do"')

            } catch (error) {

            }
        }
    })
}